﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LiveCodeServer
{
    public class Project
    {
        public enum ProjectRole {Admin};

        private int iID = -1;
        private string sName = "";

        private string sServerPath = "";

        private List<string> lstRelativeFilePaths = new List<string>();
        private Dictionary<User, string> dicUserClientPaths = new Dictionary<User, string>();
        private List<User> lstOnlineUsers = new List<User>();


        /// <summary>
        /// A dictionary containing user ids and their roles
        /// </summary>
        private Dictionary<int, ProjectRole> dicUsers = new Dictionary<int, ProjectRole>();

        public int ID
        {
            get
            {
                return this.iID;
            }
        }
        public List<string> RelativeFilePaths
        {
            get
            {
                return this.lstRelativeFilePaths;
            }
        }
        public Dictionary<User, string> UserClientPaths
        {
            get
            {
                return this.dicUserClientPaths;
            }
        }
        public List<User> OnlineUsers
        {
            get
            {
                return this.lstOnlineUsers;
            }
        }

        /// <summary>
        /// Creates a new instance of the project using the input xml data
        /// </summary>
        /// <param name="xmlProjectNode">XML node containing project information</param>
        public Project(int iProjectID, string sProjectName, XmlNode xmlProjectNode, User onlineUser)
        {
            try
            {
                //Make sure the project node has a user and files nodes
                if (xmlProjectNode.SelectNodes("User").Count > 0 && xmlProjectNode.SelectSingleNode("Files") != null)
                {
                    this.lstOnlineUsers = new List<User>();
                    this.lstOnlineUsers.Add(onlineUser);

                    this.iID = iProjectID;
                    this.sName = sProjectName;

                    XmlNode xmlProjectFiles = xmlProjectNode.SelectSingleNode("Files");

                    //Get project information
                    this.sServerPath = xmlProjectFiles.SelectSingleNode("ServerLocation").InnerText;
                    string sUserClientPath = xmlProjectNode.SelectSingleNode("User[@id='" + onlineUser.ID.ToString() + "']/ClientLocation").InnerText;
                    this.dicUserClientPaths.Add(onlineUser, sUserClientPath);

                    XmlNodeList xmlRelativePaths = xmlProjectFiles.SelectNodes("RelativePaths/FilePath");
                    this.lstRelativeFilePaths = new List<string>();
                    foreach (XmlNode path in xmlRelativePaths)
                    {
                        string sRelativePath = path.InnerText;
                        lstRelativeFilePaths.Add(sRelativePath);
                    }
                }
                else
                {
                    throw new XmlException("Invalid xml node for project");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void AddUser(User user, string sClientLocation)
        {
            this.lstOnlineUsers.Add(user);
            this.dicUserClientPaths.Add(user, sClientLocation);
        }
    }
}
